package com.podium.web.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.podium.auth.utils.SecurityUtils;
import com.podium.comment.utils.StringUtils;
import com.podium.web.system.entity.SysRole;
import com.podium.web.system.mapper.SysRoleMapper;
import com.podium.web.system.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 邵文泽
 * @description 角色表
 * @date Tue Apr 29 08:39:29 CST 2025
 */
@Slf4j
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Override
    public IPage<SysRole> page(Page<SysRole> page, SysRole sysRole) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SysRole::getDelFlag, 0)
                .like(StringUtils.hasLength(sysRole.getName()), SysRole::getName, sysRole.getName())
                .like(StringUtils.hasLength(sysRole.getRoleKey()), SysRole::getRoleKey, sysRole.getRoleKey())
                .orderByAsc(SysRole::getSort)
                .orderByDesc(SysRole::getId)
        ;
        return super.page(page, queryWrapper);
    }

    @Override
    public List<SysRole> list(SysRole sysRole) {
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SysRole::getDelFlag, 0)
                .like(StringUtils.hasLength(sysRole.getName()), SysRole::getName, sysRole.getName())
                .like(StringUtils.hasLength(sysRole.getRoleKey()), SysRole::getRoleKey, sysRole.getRoleKey())
                .orderByAsc(SysRole::getSort)
                .orderByDesc(SysRole::getId)
        ;
        return super.list(queryWrapper);
    }

    @Override
    public SysRole getById(Long id) {
        return super.getById(id);
    }

    @Override
    public boolean save(SysRole sysRole) {
        sysRole.setDelFlag(0L);
        sysRole.setCreateBy(SecurityUtils.getUsername());
        sysRole.setCreateTime(LocalDateTime.now());
        return super.save(sysRole);
    }

    @Override
    public boolean updateById(SysRole sysRole) {
        sysRole.setUpdateBy(SecurityUtils.getUsername());
        sysRole.setUpdateTime(LocalDateTime.now());
        return super.updateById(sysRole);
    }

    @Override
    public void removeById(Long id) {
        super.removeById(id);
    }

    @Override
    public List<SysRole> listByIds(List<Long> roleIds) {
        return super.listByIds(roleIds);
    }

}